# Programmable completion for Plowshare commands under bash 3.x
# Copy this file to /etc/bash_completion.d/plowshare

have plowup &&
_plowup()
{
    local cur prev arg
    COMPREPLY=()

    # fix colon separator issue. See item E13 at http://tiswww.case.edu/php/chet/bash/FAQ
    COMP_WORDBREAKS=${COMP_WORDBREAKS//:}

    cur=`_get_cword`
    prev=`_get_pword`

    case "$prev" in
        -v)
            COMPREPLY=( $( compgen -W '0 1 2 3 4' -- $cur ) )
            return 0
            ;;
        -i|--interface)
            _available_interfaces -a
            return 0
            ;;
        -a|-d|-p|--auth|--description|--link-password|--name-prefix|--name-suffix|-l|--limit-rate|-r|--max-retries)
            # argument required but no completions available
            return 0
            ;;
    esac

    if [[ "$cur" == -* ]]; then
        COMPREPLY=( $( compgen -W '--help --version --quiet \
            -v -q -i -l -r \
            --limit-rate --interface --max-retries --name-prefix --name-suffix \
            --no-curlrc --no-plowsharerc' -- $cur ) )
    else
        local m list
        list=$(sed -ne '/^[^#].*|[[:space:]]*upload[[:space:]]*|/p' \
            /usr/local/share/plowshare/modules/config | cut -d'|' -f1)

        # check for already given module name
        # note: _get_first_arg is not accurate enough (option with argument)
        for m in $list; do
            for (( i=0; i < ${#COMP_WORDS[@]}; i++ )); do
                if [ "${COMP_WORDS[i]}" == $m ]; then
                    _filedir
                    return 0
                fi
            done
        done

        COMPREPLY=( $(compgen -W "$list" -- $cur ) )
    fi
} &&
complete -F _plowup plowup

have plowdown &&
_plowdown()
{
    local cur prev

    COMPREPLY=()
    cur=`_get_cword`
    prev=`_get_pword`

    case "$prev" in
        -v)
            COMPREPLY=( $( compgen -W '0 1 2 3 4' -- $cur ) )
            return 0
            ;;
        -a|-b|-p|--auth-free|--auth|--link-password|--captchatrader|-l|--limit-rate|-r|--max-retries)
            # argument required but no completions available
            return 0
            ;;
        -o|--output-directory|--temp-directory)
            _filedir -d
            return 0
            ;;
        -i|--interface)
            _available_interfaces -a
            return 0
            ;;
        --captchamethod)
            COMPREPLY=( $( compgen -W 'none prompt' -- $cur ) )
            return 0
            ;;
    esac

    if [[ "$cur" == -* ]]; then
        COMPREPLY=( $( compgen -W '--help --version --quiet \
            -v -q -i -l -o -m -x -t -r \
            --limit-rate --interface --check-link --mark-downloaded --no-overwrite \
            --output-directory --temp-directory --timeout --max-retries --no-extra-wait \
            --fallback --captchatrader --captchamethod --no-curlrc --no-plowsharerc' -- $cur ) )
    else
        _filedir
    fi
} &&
complete -F _plowdown plowdown

have plowdel &&
_plowdel()
{
    local cur prev

    COMPREPLY=()
    cur=`_get_cword`
    prev=`_get_pword`

    case "$prev" in
        -v)
            COMPREPLY=( $( compgen -W '0 1 2 3 4' -- $cur ) )
            return 0
            ;;
        -i|--interface)
            _available_interfaces -a
            return 0
            ;;
    esac

    if [[ "$cur" == -* ]]; then
        COMPREPLY=( $( compgen -W '--help --version --quiet \
            -v -q -i --interface  --no-plowsharerc' -- $cur ) )
    else
        # URL pattern expected
        _filedir
    fi
} &&
complete -F _plowdel plowdel

have plowlist &&
_plowlist()
{
    local cur prev

    COMPREPLY=()
    cur=`_get_cword`
    prev=`_get_pword`

    case "$prev" in
        -v)
            COMPREPLY=( $( compgen -W '0 1 2 3 4' -- $cur ) )
            return 0
            ;;
        -i|--interface)
            _available_interfaces -a
            return 0
            ;;
    esac

    if [[ "$cur" == -* ]]; then
        COMPREPLY=( $( compgen -W '--help --version --quiet \
            -v -q -i -r --interface  --recursive' -- $cur ) )
    else
        # URL pattern expected
        _filedir
    fi
} &&
complete -F _plowlist plowlist

# ex: ts=4 sw=4 et filetype=sh
